﻿using System.Data.SqlClient;
using GeneralLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GeneralLibs.Lists;
using System.Collections;
using Security;

namespace Portal.DanhMuc
{
    public partial class FrmDanhMucDaiDien : FrmBaseListForm
    {
        public FrmDanhMucDaiDien()
        {
            InitializeComponent();
        }

        private void FrmDanhMucDaiDien_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            gridDSDoiTuong.Visible = false;
            tlvDanhMucDaiDien.OptionsBehavior.Editable = false;
            //tlvDanhMucDaiDien.ParentFieldName = "MaDaiDienChaID";
            tlvDanhMucDaiDien.ExpandAll();
        }
        void Instance_Change()
        {
            LoadData();
        }
        public override void LoadData()
        {
            try
            {
                tlvDanhMucDaiDien.DataSource = DanhMucDaiDien.Instance.DataView;
                InitGridColumnNews();
            }
            catch(Exception ex)
            {
            }
        }
        public void InitGridColumnNews()
        {
            foreach (DevExpress.XtraTreeList.Columns.TreeListColumn column in tlvDanhMucDaiDien.Columns)
            {
                column.Visible = false;
            }

            tlvDanhMucDaiDien.Columns["MaDaiDien"].Caption = "Mã";
            tlvDanhMucDaiDien.Columns["MaDaiDien"].Visible = true;
            tlvDanhMucDaiDien.Columns["MaDaiDien"].VisibleIndex = 1;
            tlvDanhMucDaiDien.Columns["DienGiai"].Caption = "Diễn giải";
            tlvDanhMucDaiDien.Columns["DienGiai"].Visible = true;
            tlvDanhMucDaiDien.Columns["DienGiai"].VisibleIndex = 2;
        }

        private void TlvDanhMucDaiDien_DoubleClick(object sender, EventArgs e)
        {
            Edit();        
        }
        public override void CreateNew()
        {
            try
            {
                FrmEditDaiDien frm = new FrmEditDaiDien(null);
                frm.ShowDialog();
            }
            catch(Exception ex)
            {
            }
        }
        protected override void Edit()
        {
            try
            {
                Guid id = (Guid)tlvDanhMucDaiDien.Selection[0]["ID"];
                FrmEditDaiDien frm = new FrmEditDaiDien(DanhMucDaiDien.Instance.GetByID(id));
                frm.ShowDialog();
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        protected override void Delete()
        {
            try
            {
                Guid id = (Guid)tlvDanhMucDaiDien.Selection[0]["ID"];
                DaiDien deletedObj = DanhMucDaiDien.Instance.GetByProperty("ID", id);
                if (deletedObj != null)
                {
                    if (MessageBox.Show("Bạn có thực sự muốn xóa đại diện?", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.No)
                        return;
                    else
                        deletedObj.DeleteFromDB();
                }
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
